/*
 * Screen.c
 *
 *  Created on: Jun 19, 2021
 *      Author: user
 */

#include "Screen.h"

#include "stm32f4xx_ll_usart.h"


// 是否将串口重定向至虚拟串口屏
#define VIRTUAL_USART 1

#if VIRTUAL_USART
  #define USART_USEING USART1
#else
  #define USART_USEING USART3
#endif

static void TransmitData8(USART_TypeDef *USARTx, uint8_t Value)
{
	while (!LL_USART_IsActiveFlag_TXE(USARTx)) // 等待上次发送结束
	{
	}
	LL_USART_TransmitData8(USARTx, Value);
}

void displayValue(uint16_t addr, float data)
{
	//next get data high & low bits
	uint16_t temp;
	temp = (uint16_t) (data * 100);
	uint8_t high8, low8;
	high8 = (temp & 0xff00) >> 8;
	low8 = temp & 0x00ff;
	//next get address high & low bits
	uint8_t addr_high, addr_low;
	addr_high = (addr & 0xff00) >> 8;
	addr_low = (addr & 0x00ff);
	//send messages
	TransmitData8(USART_USEING, 0xA5);
	TransmitData8(USART_USEING, 0x5A);
	TransmitData8(USART_USEING, 0x05);
	TransmitData8(USART_USEING, 0x82);
	TransmitData8(USART_USEING, addr_high);
	TransmitData8(USART_USEING, addr_low);
	TransmitData8(USART_USEING, high8);
	TransmitData8(USART_USEING, low8);
}

void displayIcon(uint16_t addr, uint16_t pic_num)
{
	//next get pic order num high & low bits
	uint8_t picn_high, picn_low;
	picn_high = (pic_num & 0xff00) >> 8;
	picn_low = (pic_num & 0x00ff);
	//next get address high & low bits
	uint8_t addr_high, addr_low;
	addr_high = (addr & 0xff00) >> 8;
	addr_low = (addr & 0x00ff);
	//send messages
	TransmitData8(USART_USEING, 0xA5);
	TransmitData8(USART_USEING, 0x5A);
	TransmitData8(USART_USEING, 0x05);
	TransmitData8(USART_USEING, 0x82);
	TransmitData8(USART_USEING, addr_high);
	TransmitData8(USART_USEING, addr_low);
	TransmitData8(USART_USEING, picn_high);
	TransmitData8(USART_USEING, picn_low);
}

